using System;
using System.IO;
using System.Linq;
using System.Text;

using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

using Pronghorn.Core;

namespace ViewEngine_Tests
{
	[TestFixture]
	public class SimpleMinifier_Tests
	{
		#region Public Methods

		[Test]
		public void Minify_a_CSS_file()
		{
			var sb = new StringBuilder();
			sb.AppendLine("  ");
			sb.AppendLine("/******************************************/");
			sb.AppendLine("/** standard variables (for all sites)");
			sb.AppendLine("Page Group");
			sb.AppendLine("%%Page.BC%% - Background Color");
			sb.AppendLine("%%Page.TC%% - Text Color");
			sb.AppendLine("%%Page.LC%% - Link Color");
			sb.AppendLine("%%Page.RC%% - Rollover Color");
			sb.AppendLine("%%Page.SC%% - Secondary Color");
			sb.AppendLine(" ");
			sb.AppendLine("Box Frame Group");
			sb.AppendLine("%%BoxFrame.BC%% - Box Frame Background Color");
			sb.AppendLine("%%BoxFrame.TC%% - Box Frame Text Color");
			sb.AppendLine("Box Body Group");
			sb.AppendLine("%%Boxbg.BC%% - Box Background Color");
			sb.AppendLine("%%Boxbg.TC%% - Box Text Color");
			sb.AppendLine("%%Boxbg.LC%% - Box Link Color");
			sb.AppendLine("%%Boxbg.RC%% - Box Hover Color");
			sb.AppendLine("%%Boxbg.MedBC%% - Box Medium Color");
			sb.AppendLine("  ");
			sb.AppendLine("********************************************/");
			sb.AppendLine(" ");
			sb.AppendLine("/*** HTML Tags ***/");
			sb.AppendLine("body { color: #%%Page.TC%%; font-family: Arial, sans-serif; background-color: #%%Page.BC%%; margin: 0px; text-align: center; }");
			sb.AppendLine("/*td { color: #%%Page.TC%%; font-family: Arial, sans-serif; }*/");
			sb.AppendLine("a { color: #%%Page.LC%%; }");
			sb.AppendLine("a:hover { color: #%%Page.RC%%; }");
			sb.AppendLine("select { font-size: 11px; }");
			sb.AppendLine("form { padding: 0px; }");
			sb.AppendLine("");
			sb.AppendLine("/* clear: stop floating after this element */");
			sb.AppendLine(".clear:after { content: \".\"; display: block; height: 0; font-size: 0; clear: both; visibility: hidden; }");
			sb.AppendLine(".clear { min-height: 1px; }");
			sb.AppendLine("* html .clear { height: 1px; }");
			sb.AppendLine("");
			sb.AppendLine("/* expand: expand to enclose all children even if they all float */");
			sb.AppendLine(".expand { overflow: hidden; }");
			sb.AppendLine("* html .expand { zoom: 1; }");
			sb.AppendLine("");
			sb.AppendLine("/*** Generic Classes ***/");
			sb.AppendLine(".SureError { background-color: #FFC3C3; border: 1px solid red; color: Black; font-weight: bold; padding: 3px; display: table-cell; }");
			sb.AppendLine(".SureNoticeBox { background-color: #FFC3C3; border: 1px solid red; color: Black; font-size: 13px; padding: 0px 10px; text-align: center; margin-bottom: 20px; }");
			sb.AppendLine(".SureNoticeBox a { color: Black; }");
			sb.AppendLine(".SureNoticeBox h3 { font-size: 15px; font-weight: bold; }");

			var minifier = new SimpleFileMinifier();
			StringWriter writer = new StringWriter();
			minifier.Minify(sb.ToString().Split(new[] { "\r\n" }, StringSplitOptions.None).ToList(), writer);

			var result = writer.ToString();
			var lines = result.Split(new[] { "\r\n" }, StringSplitOptions.None);
			Assert.That(lines.ToList().Count, Is.EqualTo(15));
		}

		[Test]
		public void Minify_a_JavaScript_file()
		{
			var sb = new StringBuilder();
			sb.AppendLine("  ");
			sb.AppendLine(" ");
			sb.AppendLine(" /*");
			sb.AppendLine(" *  this is a comment line");
			sb.AppendLine(" * @param this is a param comment");
			sb.AppendLine("*/");
			sb.AppendLine(" /*");
			sb.AppendLine("   this is a comment line");
			sb.AppendLine("  @param this is a param comment");
			sb.AppendLine("*/");
			sb.AppendLine("/*****************  Change to use prototype???    *********************************************/");
			sb.AppendLine(" function dosomethingcool(){");
			sb.AppendLine(" alert('my cool code');");
			sb.AppendLine(" var x = (1 * 1);");
			sb.AppendLine("} //A comment here");
			sb.AppendLine("  ");
			sb.AppendLine("// remove this comment");

			var minifier = new SimpleFileMinifier();
			StringWriter writer =  new StringWriter();
			minifier.Minify(sb.ToString().Split(new[] { "\r\n" }, StringSplitOptions.None).ToList(), writer);

			var result = writer.ToString();
			var lines = result.Split(new [] {"\r\n"}, StringSplitOptions.None);
			Assert.That(lines.ToList().Count,Is.EqualTo(5));
		}

		#endregion Public Methods
	}
}